package com.common.proxy;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.log4j.Logger; 
//
//import com.xuka.domain.T_puser;
//import com.xuka.domain.T_user_detail;
//import com.xuka.service.T_puserService;
//import com.xuka.service.T_saller_detailService;
//import com.xuka.service.T_user_detailService;

public class EjbProxy {

	private static EjbProxy instance = null;
	private static final String serverType = "weblogic10";  // weblogic10,jboss4,websphere6
	private static final String url = "";  // t3://localhost:7001, localhost:1099, iiop://localhost
	private static InitialContext ctx = null;
	private static HashMap ejbCache  =  null;
	private static Logger log = Logger.getLogger(EjbProxy.class.getName()); 
	
	  /**
	   * ��ʼ���趨˽��Ȩ�޵Ĺ�����
	   * @throws 
	   */
	  private EjbProxy(){
	    //resetCache();
	    //setAll();
		ejbCache  =  new HashMap();  
	  }

	  /**
	   * �趨���еĻ������ķ����ӿ�
	   * @param 
	   * @throws 
	   */
	  private void setAll(){
		  ejbCache  =  new HashMap();
	  }
	  /**
	   * ��ʵ����ʽʵ�ֶԻ�������ʵ��������
	   * @throws 
	   * @return
	   */
	  public static EjbProxy getInstance() {
	    if (instance == null) {
	      System.out.println("instance is new called");
	      log.debug("instance is new called");
	      instance = new EjbProxy();
	    }

	    return instance;
	  }


	  /**
	   * �����趨���е�ϵͳ�������
	   */
	  public static void resetCache() {
		  System.out.println("-------------------not error flag , is  ejb  cache reset start-------------------");
		  log.error("------------------- not error flag , is ejb  cache reset start-------------------");
		  ejbCache = null;
		  ejbCache  =  new HashMap();
		  System.out.println("-----------------not error flag , isejb  cache reset end-------------------");
		  log.error("------------------- not error flag , is ejb  cache reset end-------------------");

	  }
	  
	//��ʼ������������
	private static InitialContext getInitialContext() throws Exception{

		if(ctx == null){
			Properties props = new Properties();
			Properties weblogicProp = new ReadConfig().init("weblogic.properties");
			//�жϵ�ǰ��ejb����
			//weblogic10
			if(serverType.equalsIgnoreCase("weblogic10")){
				props.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
				props.put(Context.PROVIDER_URL, weblogicProp.get("weblogic10url") );
				props.put(Context.SECURITY_PRINCIPAL, weblogicProp.get("weblogic10user"));
				props.put(Context.SECURITY_CREDENTIALS, weblogicProp.get("weblogic10password"));
				
			}else if(serverType.equalsIgnoreCase("jboss4")){  //jboss4
				props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
				props.setProperty("java.naming.provider.url", url);
				props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); 	
				
			}else if(serverType.equalsIgnoreCase("websphere6")){  //websphere6
				props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
				props.setProperty(Context.PROVIDER_URL, url);
				
			}else{
				System.out.println("----------sorry,  config is not correct!-----------");
			}

			ctx = new InitialContext(props);
		}
		return ctx;		
	}

	//client����ͨ���˷�������ejb�ӿڣ�Ȼ��ת����Ϊ��Ҫ���ض�Զ�̽ӿھͿ�����
	public static Object lookup(String jndi){
		Object value = null;
		try{
			//���ʵ����û�г�ʼ��������ó�ʼ��
			if(instance==null){
				instance = getInstance();
			}
			if(ejbCache.containsKey(jndi.trim())){
				log.debug("---------lookup ejb jndi already exist!----------");
				System.out.println("---------lookup ejb jndi already exist!----------");				
				return  getInstance().ejbCache.get(jndi.trim());
			}else if(!ejbCache.containsKey(jndi)){
				InitialContext  ctx = getInitialContext();
				value = ctx.lookup(jndi);
				log.debug("---------lookup ejb jndi:"+value);
				System.out.println("---------lookup ejb jndi:"+value);				
				ejbCache.put(jndi, value);
//				Iterator it = ejbCache.keySet().iterator();
//				while(it.hasNext()){
//					Object obj= it.next();
//					log.debug("----------ejbCache.value="+ obj +"------legth="+ obj.toString());
//				}
			}
		}catch(Exception e){
			e.printStackTrace();
			//System.out.println("---------Ejb2Proxy lookup ejb error="+e);
			log.error("---------EjbProxy lookup ejb error="+e);
			System.out.println("---------EjbProxy lookup ejb error="+e);	
			//�˴����Է�ֹweblogic10���м����������������ejb�ͻ��˻�ʧЧ�������Զ�ɾ������ͻ��˻��棬
			//�Ϳ���ͨ��һ��ʧ�ܺ����²��Ҳ������µģ����������Ͳ���ʧ���ˣ��˴�����ejb3
			//Ҳ����ͨ��resetCache�����web app server�е�ejb3�ͻ��˻���
			if(ejbCache.containsKey(jndi)){
				//System.out.println("---------EjbProxy cache ɾ��ʧЧ�� ejb3�ͻ��˻���-----------jndi="+jndi);
				log.error("---------EjbProxy cache ɾ��ʧЧ�� ejb3�ͻ��˻���-----------jndi="+jndi);
				System.out.println("---------EjbProxy cache ɾ��ʧЧ�� ejb3�ͻ��˻���-----------jndi="+jndi);					
				ejbCache.remove(jndi);
			}

		}
		return value;
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{

		//for(int i= 0; i<10;i++){
//			//InitialContext  ctx = new InitialContext();
//			//InitialContext  ctx = getInitialContext();
//			//ITestEjb2Remote ejb = (ITestEjb2Remote) ctx.lookup("TestEjb2#ITestEjb2Remote");
//			ITestEjb2Remote ejb = (ITestEjb2Remote)lookup("TestEjb2#ITestEjb2Remote");
//			System.out.println("..........ejb res="+ ejb.foo("davexin"));
//			T_function t = new T_function();
//			t = (T_function)ejb.findByPrimaryKey(new Integer(1));
//			System.out.println("..........ejb res startNo="+t.startNo);
//			System.out.println("..........ejb res endNo="+t.endNo);
//			System.out.println("..........ejb res getFunc_name="+t.getFunc_name());
//			System.out.println("..........ejb res getIsuse="+t.getIsuse());
//			System.out.println("..........ejb res getMemo="+t.getMemo());
//			System.out.println("..........ejb res obj="+t.endNo);

//			//InitialContext  ctx = new InitialContext();
//			//InitialContext  ctx = getInitialContext();
//			//ITestEjb2Remote ejb = (ITestEjb2Remote) ctx.lookup("TestEjb2#ITestEjb2Remote");
//			T_dealer_accountService ejb = (T_dealer_accountService)lookup("T_dealer_accountBean#com.xuka.service.T_dealer_accountService");
//			System.out.println("..........ejb res="+ ejb.foo("davexin"));
//			T_function t = new T_function();
//			t = (T_function)ejb.findByPrimaryKey(new Integer(1));
//			System.out.println("..........ejb res startNo="+t.startNo);
//			System.out.println("..........ejb res endNo="+t.endNo);
//			System.out.println("..........ejb res getFunc_name="+t.getFunc_name());
//			System.out.println("..........ejb res getIsuse="+t.getIsuse());
//			System.out.println("..........ejb res getMemo="+t.getMemo());
//			System.out.println("..........ejb res obj="+t.endNo);

//		    InitialContext  ctx = getInitialContext();
//		    String jndi ="T_user_detailServiceBean#com.xuka.service.T_user_detailService";
//		    Object value = ctx.lookup(jndi);
//		    T_user_detailService service = (T_user_detailService) value;
//		    T_user_detail vo = new T_user_detail();
//		    vo.setDealerid(222222);
//		    vo.setParentid("");
		    
		    //System.out.println("..........ejb res obj="+service.queryByDealeridandPd(vo));
		    //System.out.println("..........ejb res obj="+service.checkuser(vo));
		    
//			T_puserService t_puserService = (T_puserService)EjbProxy.lookup("T_puserServiceBean#com.xuka.service.T_puserService");
//			T_puser t_puser = new T_puser();
//			System.out.println("..........ejb res obj="+t_puserService.findAllRationBySysno());
//			System.out.println("..........ejb res obj="+t_puserService.getAllCitys(null, 0));			
			
//			T_user_detailService s2 = (T_user_detailService)EjbProxy.lookup("T_user_detailServiceBean#com.xuka.service.T_user_detailService");
//			T_user_detail obj = new T_user_detail();
//			obj.setAcctip("10.3.1.53");					
//			System.out.println("..........ejb res obj="+s2.wangsy_add(obj));	
			
		//T_saller_detailService s2 = (T_saller_detailService)EjbProxy.lookup("T_saller_detailServiceBean#com.xuka.service.T_saller_detailService");
		//T_user_detail obj = new T_user_detail();
		//obj.setAcctip("10.3.1.53");
		//obj.setUsertype("L");
		
		//System.out.println("..........ejb res obj="+s2.findByCity(obj, 10, 0));	
			
			
			//}
	}

}
